package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.SystemMenu;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.ISystemMenuService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("systemMenu")
public class SystemMenuController {
    @Autowired
    private ISystemMenuService systemMenuService;

    @RequestMapping("")
    public String index(){
        return "systemMenu/list";
    }

    @RequiresPermissions("systemMenu:view")
    @PermissionName("菜单列表")
    @RequestMapping("list")
    @ResponseBody
    public PageResult list(QueryObject qo){
        return systemMenuService.query(qo);
    }

   /* @RequestMapping("queryRootMenu")
    @ResponseBody
    public List<SystemMenu> queryRootMenu(){

        //从session获取登录成功时候缓存的菜单集合
        return (List<SystemMenu>) SecurityUtils.getSubject()
                .getSession().getAttribute(SystemMenuUtil.SYSTEM_MENU_IN_SESSION);
    }*/
    @RequestMapping("queryParentMenu")
    @ResponseBody
    //查询父类菜单
    public List<SystemMenu> queryParentMenu(){

        //从session获取登录成功时候缓存的菜单集合
        return systemMenuService.queryRootMenu();
    }
    @RequestMapping("queryAllSystemMenu")
    @ResponseBody
    //查询全部菜单
    public List<SystemMenu> queryAllSystemMenu(){

        //从session获取登录成功时候缓存的菜单集合
        return systemMenuService.list();
    }

    @RequestMapping("queryChildrenMenu")
    @ResponseBody
    //查询父类菜单
    public List<SystemMenu> queryChildrenMenu(Long parentId){
        //从session获取登录成功时候缓存的菜单集合
        return systemMenuService.queryChildrenMenu(parentId);
    }


    @RequiresPermissions("systemMenu:save")
    @PermissionName("菜单保存")
    @RequestMapping("save")
    @ResponseBody
    public JsonResult save(SystemMenu entity){
        JsonResult result = new JsonResult();
        try {
            systemMenuService.save(entity);
        }catch (Exception e){
            e.printStackTrace();
            result.markMsg("保存操作失败");
        }
        return result;
    }
    @RequiresPermissions("systemMenu:update")
    @PermissionName("编辑菜单")
    @RequestMapping("update")
    @ResponseBody
    public JsonResult update(SystemMenu entity){
        JsonResult result = new JsonResult();
        try {
            systemMenuService.update(entity);
        }catch (Exception e){
            e.printStackTrace();
            result.markMsg("编辑信息失败");
        }
        return result;
    }

    @RequiresPermissions("systemMenu:delete")
    @PermissionName("删除菜单")
    @RequestMapping("delete")
    @ResponseBody
    public JsonResult delete(Long id){
        JsonResult result = new JsonResult();
        try {
            systemMenuService.delete(id);
        }catch (Exception e){
            e.printStackTrace();
            result.markMsg("删除信息失败");
        }
        return result;
    }


}
